package com.chinatsp.onlineapp.ui.weather;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.PreferenceUtils;

import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.BaseActivity;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.bean.WeatherService;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean;
import com.chinatsp.onlineapp.presenter.WeatherServiceDao;

public class WeatherMoreActivity extends BaseActivity {

	private Context mContext;
	private ListView lv_city1;
	private ListView lv_city2;
	private ListView lv_city3;
	private RespDataBean data1, data2, data3;
	private WeatherServiceDao weatherServiceDao;
	private List<Integer> list_citys;
	private View topView1, topView2, topView3;
	private ImageView iv_refresh;
	private TextView t1_position;
	private TextView t2_position;
	private TextView t3_position;
	private TextView t1_date;
	private TextView t2_date;
	private TextView t3_date;
	private TextView t1_cweek;
	private TextView t2_cweek;
	private TextView t3_cweek;
	private ImageView iv1;
	private ImageView iv2;
	private ImageView iv3;
	private TextView t1_cweather;
	private TextView t2_cweather;
	private TextView t3_cweather;
	private TextView t1_ctemp;
	private TextView t2_ctemp;
	private TextView t3_ctemp;

	@Override
	public void onRequestSuccess(int requestCode) {
		super.onRequestSuccess(requestCode);
		showProgress(false);
		LogUtil.e("request Code: " + requestCode);

		if (requestCode == list_citys.get(0)) {
			data1 = weatherServiceDao.getmWeatherService().getResp_data();
			if (data1.getCurrent_conditions() != null) {
				t1_position.setText(data1.getCity_name());
				t1_date.setText(data1.getCurrent_conditions().getWeather_date());
				// t1_cweek.setText("");
				// iv1.setImageResource()
				t1_cweather.setText(data1.getCurrent_conditions()
						.getWeather_condition());
				String temp1 = data1.getCurrent_conditions()
						.getLow_temperature()
						+ "/"
						+ data1.getCurrent_conditions().getHigh_temperature();
				t1_ctemp.setText(temp1);
			}
			WeatherCitysAdapter adapter = new WeatherCitysAdapter(mContext,
					data1);
			if (lv_city1.getHeaderViewsCount() == 0) {
				lv_city1.addHeaderView(topView1);
			}
			lv_city1.setAdapter(adapter);

		} else if (requestCode == list_citys.get(1)) {
			data2 = weatherServiceDao.getmWeatherService().getResp_data();
			if (data2.getCurrent_conditions() != null) {
				t2_position.setText(data2.getCity_name());
				t2_date.setText(data2.getCurrent_conditions().getWeather_date());
				// t2_cweek.setText("");
				// iv2.setImageResource()
				t2_cweather.setText(data2.getCurrent_conditions()
						.getWeather_condition());
				String temp2 = data2.getCurrent_conditions()
						.getLow_temperature()
						+ "/"
						+ data2.getCurrent_conditions().getHigh_temperature();
				t2_ctemp.setText(temp2);
			}
			WeatherCitysAdapter adapter = new WeatherCitysAdapter(mContext,
					data2);
			if (lv_city2.getHeaderViewsCount() == 0) {
				lv_city2.addHeaderView(topView2);
			}
			lv_city2.setAdapter(adapter);

		} else {
			data3 = weatherServiceDao.getmWeatherService().getResp_data();
			if (data3.getCurrent_conditions() != null) {
				t3_position.setText(data3.getCity_name());
				t3_date.setText(data3.getCurrent_conditions().getWeather_date());
				// t2_cweek.setText("");
				// iv2.setImageResource()
				t3_cweather.setText(data3.getCurrent_conditions()
						.getWeather_condition());
				String temp3 = data3.getCurrent_conditions()
						.getLow_temperature()
						+ "/"
						+ data3.getCurrent_conditions().getHigh_temperature();
				t3_ctemp.setText(temp3);
			}
			WeatherCitysAdapter adapter = new WeatherCitysAdapter(mContext,
					data3);
			if (lv_city3.getHeaderViewsCount() == 0) {
				lv_city3.addHeaderView(topView3);
			}

			lv_city3.setAdapter(adapter);
		}
	}

	@Override
	public void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_weather_detail);
		init();
	}

	private void init() {
		getData();
		initView();
	}

	private void getData() {
		list_citys = new ArrayList<Integer>();
		mContext = this;
		data1 = new RespDataBean();
		data2 = new RespDataBean();
		data3 = new RespDataBean();
		WeatherService weatherService = new WeatherService();
		weatherServiceDao = new WeatherServiceDao(mContext, this);

	}

	private void initView() {
		ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});
		lv_city1 = (ListView) findViewById(R.id.lv_city1);
		lv_city2 = (ListView) findViewById(R.id.lv_city2);
		lv_city3 = (ListView) findViewById(R.id.lv_city3);
		
		list_citys.add(1);
		list_citys.add(2);
		list_citys.add(3);
		String defaultCity = PreferenceUtils.getPreference(Constant.DEFAULT_CITY);
		String selectCity1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1);
		String selectCity2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2);
		LogUtil.e(defaultCity + selectCity1+ selectCity2);
		if (selectCity1.contains("#")) {
			lv_city2.setVisibility(View.GONE);
		}else if(selectCity2.contains("#")){
			lv_city3.setVisibility(View.GONE);
		}
		
		weatherServiceDao.getWeatherService(defaultCity, list_citys.get(0));
		weatherServiceDao.getWeatherService(selectCity1, list_citys.get(1));
		weatherServiceDao.getWeatherService(selectCity2, list_citys.get(2));
		
		topView1 = LayoutInflater.from(mContext).inflate(
				R.layout.item_list_top, null, false);
		topView2 = LayoutInflater.from(mContext).inflate(
				R.layout.item_list_top, null, false);
		topView3 = LayoutInflater.from(mContext).inflate(
				R.layout.item_list_top, null, false);

		t1_position = (TextView) topView1.findViewById(R.id.tv_cposition);
		t2_position = (TextView) topView2.findViewById(R.id.tv_cposition);
		t3_position = (TextView) topView3.findViewById(R.id.tv_cposition);
		t1_date = (TextView) topView1.findViewById(R.id.tv_cdate);
		t2_date = (TextView) topView2.findViewById(R.id.tv_cdate);
		t3_date = (TextView) topView3.findViewById(R.id.tv_cdate);
		t1_cweek = (TextView) topView1.findViewById(R.id.tv_cweek);
		t2_cweek = (TextView) topView2.findViewById(R.id.tv_cweek);
		t3_cweek = (TextView) topView3.findViewById(R.id.tv_cweek);
		iv1 = (ImageView) topView1.findViewById(R.id.imageView);
		iv2 = (ImageView) topView2.findViewById(R.id.imageView);
		iv3 = (ImageView) topView3.findViewById(R.id.imageView);
		t1_cweather = (TextView) topView1.findViewById(R.id.tv_cweather);
		t2_cweather = (TextView) topView2.findViewById(R.id.tv_cweather);
		t3_cweather = (TextView) topView3.findViewById(R.id.tv_cweather);
		t1_ctemp = (TextView) topView1.findViewById(R.id.tv_ctemp);
		t2_ctemp = (TextView) topView2.findViewById(R.id.tv_ctemp);
		t3_ctemp = (TextView) topView3.findViewById(R.id.tv_ctemp);

		iv_refresh = (ImageView) findViewById(R.id.iv_refresh);
		iv_refresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showProgress(true);
				weatherServiceDao.getWeatherService("13", list_citys.get(0));
				weatherServiceDao.getWeatherService("13", list_citys.get(1));
				weatherServiceDao.getWeatherService("13", list_citys.get(2));

			}
		});

	}
}
